import React, { Component } from 'react';
import store from '../utils/store';
import { Redirect } from 'react-router-dom';
import { USER_KEY,REDIRECT_KEY,LOGIN_URL } from '../utils/const.js';
import { Base64 } from 'js-base64'

export default class NeedLogin extends Component {
  render() {
    // 如果用户没有登陆, 自动跳转
    const user = store.get(USER_KEY);
    if (user && user.id) {
      return <div>{this.props.children}</div>;
    } else {
      let redirectUrl = LOGIN_URL+"?"+REDIRECT_KEY+"=" + Base64.encode(window.location.href)
      return <Redirect to={redirectUrl} />;
    }
  }
}
